﻿using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;

namespace DBDataContext.Model
{
    [Table]
    public class Manga : INotifyPropertyChanged, INotifyPropertyChanging
    {
        private int _mangaId;
        private string _title;
        private string _author;
        private string _summary;
        private string _link;
        private string _imageLink;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int MangaId
        {
            get { return _mangaId; }
            set
            {
                if (_mangaId != value)
                {
                    NotifyPropertyChanging("MangaId");
                    _mangaId = value;
                    NotifyPropertyChanged("MangaId");
                }
            }
        }

        [Column]
        public string Title
        {
            get { return _title; }
            set
            {
                if (_title != value)
                {
                    NotifyPropertyChanging("Title");
                    _title = value;
                    NotifyPropertyChanged("Title");
                }
            }
        }

        [Column]
        public string Author
        {
            get { return _author; }
            set
            {
                if (_author != value)
                {
                    NotifyPropertyChanging("Author");
                    _author = value;
                    NotifyPropertyChanged("Author");
                }
            }
        }

        [Column]
        public string Summary
        {
            get { return _summary; }
            set
            {
                if (_summary != value)
                {
                    NotifyPropertyChanging("Summary");
                    _summary = value;
                    NotifyPropertyChanged("Summary");
                }
            }
        }

        [Column]
        public string Link
        {
            get { return _link; }
            set
            {
                if (_link != value)
                {
                    NotifyPropertyChanging("Link");
                    _link = value;
                    NotifyPropertyChanged("Link");
                }
            }
        }

        [Column]
        public string ImageLink
        {
            get { return _imageLink; }
            set
            {
                if (_imageLink != value)
                {
                    NotifyPropertyChanging("ImageLink");
                    _imageLink = value;
                    NotifyPropertyChanged("ImageLink");
                }
            }
        }

        //#region BUILD OUT
        // Define the entity set for the collection side of the relationship.
        private EntitySet<Chapter> _chapters;

        [Association(Storage = "_chapters", OtherKey = "MangaId", ThisKey = "MangaId")]
        public EntitySet<Chapter> Chapters
        {
            get { return this._chapters; }
            set { this._chapters.Assign(value); }
        }

        // Assign handlers for the add and remove operations, respectively.
        public Manga()
        {
            _chapters = new EntitySet<Chapter>(
                new Action<Chapter>(this.attach_Chapter),
                new Action<Chapter>(this.detach_Chapter)
                );
        }

        // Called during an add operation
        private void attach_Chapter(Chapter chapter)
        {
            NotifyPropertyChanging("Chapter");
            //toDo.Category = this;
            chapter.Manga = this;
        }

        // Called during a remove operation
        private void detach_Chapter(Chapter chapter)
        {
            NotifyPropertyChanging("Chapter");
            //toDo.Category = null;
            chapter.Manga = null;
        }

        //#endregion
        #region TEMPLATE from MS
        //// Version column aids update performance.
        //[Column(IsVersion = true)]
        //private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion

        #endregion
    }

    [Table]
    public class Chapter : INotifyPropertyChanged, INotifyPropertyChanging
    {
        private int _chapterId;
        private string _title;
        private DateTime _date;
        private string _link;
        private int _status;
        private int _currentIndex; 

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int ChapterId
        {
            get { return _chapterId; }
            set
            {
                if (_chapterId != value)
                {
                    NotifyPropertyChanging("ChapterId");
                    _chapterId = value;
                    NotifyPropertyChanged("ChapterId");
                }
            }
        }

        [Column]
        public string Title
        {
            get { return _title; }
            set
            {
                if (_title != value)
                {
                    NotifyPropertyChanging("Title");
                    _title = value;
                    NotifyPropertyChanged("Title");
                }
            }
        }

        [Column]
        public DateTime Date
        {
            get { return _date; }
            set
            {
                if (_date != value)
                {
                    NotifyPropertyChanging("Date");
                    _date = value;
                    NotifyPropertyChanged("Date");
                }
            }
        }

        [Column]
        public string Link
        {
            get { return _link; }
            set
            {
                if (_link != value)
                {
                    NotifyPropertyChanging("Link");
                    _link = value;
                    NotifyPropertyChanged("Link");
                }
            }
        }

        [Column]
        public int Status
        {
            get { return _status; }
            set
            {
                if (_status != value)
                {
                    NotifyPropertyChanging("Status");
                    _status = value;
                    NotifyPropertyChanged("Status");
                }
            }
        }

        [Column]
        public int CurrentIndex
        {
            get { return _currentIndex; }
            set
            {
                if (_currentIndex != value)
                {
                    NotifyPropertyChanging("CurrentIndex");
                    _currentIndex = value;
                    NotifyPropertyChanged("CurrentIndex");
                }
            }
        }

        #region ASSOCIATION
        // Internal column for the associated ToDoCategory ID value
        [Column]
        internal int MangaId;

        // Entity reference, to identify the ToDoCategory "storage" table
        private EntityRef<Manga> _manga;

        // Association, to describe the relationship between this key and that "storage" table
        [Association(Storage = "_manga", ThisKey = "MangaId", OtherKey = "MangaId", IsForeignKey = true)]
        public Manga Manga
        {
            get { return _manga.Entity; }
            set
            {
                NotifyPropertyChanging("Manga");
                _manga.Entity = value;

                if (value != null)
                {
                    MangaId = value.MangaId;
                }

                NotifyPropertyChanging("Manga");
            }
        }
        #endregion
        
        #region TEMPLATE from MS

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion

        #endregion
    }

    [Table]
    public class Image : INotifyPropertyChanged, INotifyPropertyChanging
    {
        // Define ID: private field, public property and database column
        private int _index;
        private string _link;
        private bool _isDownloaded;
        private int _imageId;
        private int _chapterId;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int ImageId
        {
            get { return _imageId; }
            set
            {
                if (_imageId != value)
                {
                    NotifyPropertyChanging("ImageId");
                    _imageId = value;
                    NotifyPropertyChanged("ImageId");
                }
            }
        }

        [Column]
        public int Index
        {
            get { return _index; }
            set
            {
                if (_index != value)
                {
                    NotifyPropertyChanging("Index");
                    _index = value;
                    NotifyPropertyChanged("Index");
                }
            }
        }

        [Column]
        public string Link
        {
            get { return _link; }
            set
            {
                if (_link != value)
                {
                    NotifyPropertyChanging("Link");
                    _link = value;
                    NotifyPropertyChanged("Link");
                }
            }
        }

        [Column]
        public bool IsDownloaded
        {
            get { return _isDownloaded; }
            set
            {
                if (_isDownloaded != value)
                {
                    NotifyPropertyChanging("IsDownloaded");
                    _isDownloaded = value;
                    NotifyPropertyChanged("IsDownloaded");
                }
            }
        }

        [Column]
        public int ChapterId
        {
            get { return _chapterId; }
            set
            {
                if (_chapterId != value)
                {
                    NotifyPropertyChanging("ChapterId");
                    _chapterId = value;
                    NotifyPropertyChanged("ChapterId");
                }
            }
        }

        #region ASSOCIATION
        // Internal column for the associated ToDoCategory ID value
        //[Column]
        //public int ChapterId;

        // Entity reference, to identify the ToDoCategory "storage" table
        //private EntityRef<Chapter> _chapter;

        //// Association, to describe the relationship between this key and that "storage" table
        //[Association(Storage = "_chapter", ThisKey = "ChapterId", OtherKey = "ChapterId", IsForeignKey = true)]
        //public Chapter Chapter
        //{
        //    get { return _chapter.Entity; }
        //    set
        //    {
        //        NotifyPropertyChanging("Chapter");
        //        _chapter.Entity = value;

        //        if (value != null)
        //        {
        //            ChapterId = value.ChapterId;
        //        }

        //        NotifyPropertyChanging("Chapter");
        //    }
        //}
        #endregion

        #region TEMPLATE from MS
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion

        #endregion
    }
    
    public class MangaForYouDataContext : DataContext
    {
        // Pass the connection string to the base class.
        public MangaForYouDataContext(string connectionString)
            : base(connectionString)
        {
            try
            {

            }
            catch (Exception ex)
            {
               
            }
        }

        public Table<Chapter> Chapter;

        public Table<Manga> Manga;

        public Table<Image> Image;
    }
}



